package com.hqyj.system.controller;


import com.hqyj.system.model.User;
import frame.controller.BaseController;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName LoginController
 * @Description
 * @Author zlf
 * @Date 2021/9/11 13:36
 * @Version 1.0
 **/
@Controller
public class LoginController extends BaseController {

   /* @RequestMapping("/login.do")
    public String login(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user) {
        //1.接收数据；
//        System.out.println("-------------LoginController----3-----" + user);

        //3.调用业务；
        User user_db = userService.queryUserByUser(user);
//        System.out.println("-----------------" + user_db);
        //业务逻辑判断
        if (user_db != null) {
            if (user_db.getUsername().equals(user.getUsername()) && user_db.getPassword().equals(user.getPassword())) {

                //把登录用户信息保存到Session
                session.setAttribute("USER", user_db);

                //友情提示
                model.addAttribute("message", "登录成功！");
                //2.跳转页面；
                return "view/frame/main";
            } else {

                //友情提示
                model.addAttribute("message", "用户名或密码错误，请重新输入！");
                //2.跳转页面；
                return "login";
            }
        } else {

            //友情提示
            model.addAttribute("message", "用户名或密码错误，请重新输入！");
            //2.跳转页面；
            return "login";
        }


    }*/
   @RequestMapping("/login.do")
   public String login(HttpServletRequest request, HttpServletResponse response, HttpSession session, Model model, User user) {
      //如果登录失败request获取我们的认证信息,ShiroLoginLure就是shiro抛出异常名称
       String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");
       if(exceptionClassName!=null){
            //找不到账号异常
            if(UnknownAccountException.class.getName().equals(exceptionClassName)){
                model.addAttribute("massage","账号不存在！");
            }else if(IncorrectCredentialsException.class.getName().equals(exceptionClassName)){
               //密码找不到
                model.addAttribute("massage","用户名或密码错误！");
            }else{
                model.addAttribute("massage","未知异常！");
            }
       }
       //此方法不处理登陆成功，shiro认证成功会自动跳转导上一个请求路径，登录失败导login页面
       return "login";
   }

}
